package material

// 永久素材管理.

import (
    "fmt"
	"io"
	"encoding/json"
	"wxsdk/miniapp/core"
	//"github.com/astaxie/beego"
)

type Article struct {
	ThumbMediaId     string `json:"thumb_media_id"`               // 图文消息的封面图片素材id(必须是永久mediaID)
	Title            string `json:"title"`                        // 标题
	Author           string `json:"author,omitempty"`             // 作者
	Digest           string `json:"digest,omitempty"`             // 图文消息的摘要, 仅有单图文消息才有摘要, 多图文此处为空
	Content          string `json:"content"`                      // 图文消息的具体内容, 支持HTML标签, 必须少于2万字符, 小于1M, 且此处会去除JS
	ContentSourceURL string `json:"content_source_url,omitempty"` // 图文消息的原文地址, 即点击"阅读原文"后的URL
	ShowCoverPic     int    `json:"show_cover_pic"`               // 是否显示封面, 0为false, 即不显示, 1为true, 即显示
	URL              string `json:"url,omitempty"`                // !!!创建时不需要此参数!!! 图文页的URL, 文章创建成功以后, 会由微信自动生成
}

type News struct {
	Articles []Article `json:"articles,omitempty"`
}

// 新增永久图文素材.
func AddNews(accessToken string, news *News) (mediaId string, err error) {
	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token="
	var result struct {
		core.Error
		MediaId string `json:"media_id"`
	}
	serverUrl := incompleteURL + accessToken
	client := core.GetPostJSONClient()
	if _, err = client.Excute("POST", serverUrl, news, &result); err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	mediaId = result.MediaId
	return
}

//上传图文消息内的图片获取URL
func  PubUploadImg(accessToken string, input io.Reader) (url string, err error) {
	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token="
	serverUrl := incompleteURL + accessToken
	var response []byte
	virtualFileName := "tmp.jpg"
	response, err = core.PostFileByIoReader(serverUrl, "media", virtualFileName, input)
	if err != nil {
		return
	}
	var result struct {
		core.Error
		Url string `json:"url"`
	}
	err = json.Unmarshal(response, &result)
	if err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	url = result.Url
	return
}

//新增其他类型永久素材
func  PubUploadOtherMaterial(accessToken, mediaType string, input io.Reader) (mediaId, url string, err error) {
	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s&type=%s"
	serverUrl := fmt.Sprintf(incompleteURL, accessToken, mediaType)
	var response []byte
	virtualFileName := "tmp.jpg"
	if mediaType == "video" {
	    response, err = core.PostFile2FormByIoReader(serverUrl, "media", virtualFileName, input)
		if err != nil {
			return
		}
	} else {
	    response, err = core.PostFileByIoReader(serverUrl, "media", virtualFileName, input)
		if err != nil {
			return
		}
	}
	var result struct {
		core.Error
		MediaId string `json:"media_id"`
		Url     string `json:"url"`
	}
	err = json.Unmarshal(response, &result)
	if err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	mediaId = result.MediaId
	url = result.Url
	return
}

func GetMaterialByMediaId(accessToken, mediaId string) (body []byte, err error) {
	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=%s"
	url := fmt.Sprintf(incompleteURL, accessToken)
	body, err = core.DownFileByPost(url, mediaId)
	if err != nil {
		return
	}
	return
}
